<div class="step">第一步：验证您的手机号（共三步）</div>
<div class="form">
    <div class="form-group">
        <label class="require" for="mobile">手机号</label>
        <input type="text" class="form-control" id="mobile" maxlength="11">
    </div>
    <div class="form-group">
        <label class="require" for="img_code">图形验证码</label>
        <input type="text" class="form-control form-control_sm" id="img_code" maxlength="4">
        <img src="/captcha/login?6i4YzKZm" class="code j_changeCode">
    </div>
    <div class="form-group">
        <label class="require" for="sms_code">短信验证码</label>
        <input type="text" class="form-control form-control_sm ipt_disable" disabled id="sms_code"
               maxlength="6">
        <a id="getCode" class="btn btn_disable">获取</a>
    </div>
    <div class="form-group">
        <label class="radio">&nbsp;</label>
        <button type="button" class="btn j_submitMobile">下一步</button>
    </div>
</div>


    <script>
        (function ($) {
            $(function () {
                //刷新验证码
                $("body").on("click",".j_changeCode", function () {
                    var $t = $(this);
                    $t.attr("src", "/captcha/login?" + new Date().getTime());
                });
                //手机号验证
                $("body").on("blur","#mobile", function () {
                    var $ipt = $(this);
                    var val = $ipt.val();
                    if (!val || !isPhone(val)) {
                        return showErr($ipt, "请输入正确的手机号码");
                    }
                }).on("keyup","#mobile", function () {
                    var $ipt = $(this);
                    var val = $ipt.val();
                    var can = false;
                    if (val && isPhone(val)) {
                        var code = $("#img_code").val();
                        if (code && code.length === 4) {
                            can = true;
                        }
                    }

                    var $btn = $("#getCode");
                    if ($btn.html().indexOf("s") === -1) {
                        toggleDisable($btn, "btn_disable", can);
                    }
                });
                //图形验证码
                $("body").on("blur","#img_code", function () {
                    var $ipt = $(this);
                    var val = $ipt.val();
                    if (!val) {
                        return showErr($(".j_changeCode"), "请输入正确的图形验证码");
                    }
                }).on("keyup","#img_code", function () {
                    var $ipt = $(this);
                    var val = $ipt.val();
                    var can = false;
                    if (val && val.length === 4) {
                        var mobile = $("#mobile").val();
                        if (mobile && isPhone(mobile)) {
                            can = true;
                        }
                    }
                    var $btn = $("#getCode");
                    if ($btn.html().indexOf("s") === -1) {
                        toggleDisable($btn, "btn_disable", can);
                    }
                });
                //改变关联对象可操作状态
                function toggleDisable(obj, cls, bool) {
                    if (bool) {
                        obj.removeClass(cls).removeProp("disabled");
                    } else {
                        obj.addClass(cls).prop("disabled", true);
                    }
                }

                //获取验证码
                var globalCountNow = 59;
                $("body").on("click","#getCode", function () {
                    var $t = $(this);
                    if ($t.attr("class").indexOf("disable") === -1) {
                        var $code = $("#sms_code");
                        var $phone = $("#mobile");
                        var phone = $phone.val();
                        var code = $("#img_code").val();
                        toggleDisable($code, "ipt_disable", true);
                        $code.focus();
                        $.ajax({
                            url: "/phone_code",
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            data: {
                                phone: phone,
                                img_code: code,
                                type: 1,
                                from:'短信登录'
                            },
                            type: "POST",
                            dataType: "json",
                            success: function (data) {
                                var result = data["result"];
                                var msg = data["msg"];
                                if (result < 4) {
                                    toggleDisable($phone, "ipt_disable", false);
                                }
                                if (result === 0) {
                                    $t.html("已发送 （<span>60</span>）");
                                    toggleDisable($t, "btn_disable", false);
                                    var globalCountDown = setInterval(function () {
                                        if (globalCountNow > 0) {
                                            $t.find("span").html(globalCountNow);
                                            globalCountNow--;
                                        } else {
                                            toggleDisable($phone, "ipt_disable", true);
                                            toggleDisable($t, "btn_disable", true);
                                            $t.html("获取");
                                            clearInterval(globalCountDown);
                                            globalCountNow = 59;
                                        }
                                    }, 1000);
                                } else {
                                    return showErr($t, msg);
                                }
                            },
                            error: function (data) {
                                return showErr($t, "获取验证码失败");
                            }
                        });
                    }
                });
                //手机号验证
                $("body").on('click','.j_submitMobile', function () {
                    var $t = $(this);
                    var $mobile = $("#mobile");
                    var mobile = $mobile.val();
                    if (!mobile || !isPhone(mobile)) {
                        return showErr($mobile, "请输入正确的手机号码");
                    }
                    var $sms_code = $("#sms_code");
                    var sms_code = $sms_code.val();
                    if (!sms_code) {
                        return showErr($sms_code, '请输入正确的短信验证码');
                    }
                    var $img_code = $("#img_code");
                    var img_code = $img_code.val();
                    if (!img_code) {
                        return showErr($img_code, '请输入正确的图形验证码');
                    }


                    $.ajax({
                        url: "/login_by_sms",
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        data: {
                            phone: mobile,
                            code: sms_code,
                            from: '短信登录',
                            flag:2
                        },
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                             var result = data["result"];
                             var msg = data["msg"];
                             if (result === 0) {
                                 //第一步完成显示第二步页面
                                 $.get('/add_company_info_second_step', function (data, status, xhr) {
                                        $('#j_sign').html(data);
                                    }, 'html');
                             }else if(result === 100){
                                window.location.href="/user";
                             }
                        },
                        error: function (data) {
                             setTimeout(function () {
                                 location.reload();
                                 }, 3000);
                             return showErr($t, '提交失败');
                        }
                     });
                });



            });
        })(jQuery);
    </script>
